/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.collabarative1.connectionUtils.ConnectionFactory;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author Shravani
 */
public class LoginServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException,IOException
    {
        res.setContentType("text/html");
        //PrintWriter out = res.getWriter();
       ResultSet rs=null;
		String password1=null;
		String uname=req.getParameter("uname");
		char ch=uname.charAt(0);
                String password=req.getParameter("password");
		Connection con=ConnectionFactory.getConnection();
		String sql=null;
                try {
			Statement st=con.createStatement();
                        if(ch=='s')
                        {
                            sql="select password from UserDB where FIRSTNAME='"+uname+"'";
                        }else if(ch=='f')
                        {
                            sql="select password from faculty where FIRSTNAME='"+uname+"'";
                        }
                        else{
                            sql="select password from admin where FIRSTNAME='"+uname+"'";
                        }
                            rs=st.executeQuery(sql);
			System.out.println(rs);
			RequestDispatcher rd=null;
			
			if(rs.next())
			{
			password1=rs.getString(1);
			System.out.println(password1);
			System.out.println(rs.getString(1));
			}
			if(password1 != null && password1.equalsIgnoreCase(password))
			{
				char c=uname.charAt(0);
				if(c=='s'){
					rd=req.getRequestDispatcher("/StudentHome.jsp");
				}else if(c=='f')
				{
					rd=req.getRequestDispatcher("/FacultyHome.jsp");	
				}else{
					rd=req.getRequestDispatcher("/AdminHome.jsp");
				}
				rd.forward(req,res);
					
				
			}
			else{
				rd=req.getRequestDispatcher("/index.jsp");
				rd.forward(req,res);
				System.out.println("password");
				
				
				
			}
			ConnectionFactory.close(con);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			ConnectionFactory.close(con);
			e.printStackTrace();
		}
		
		
		
		}

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       // processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
